//
//  SLAddLockSetModelVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/11/27.
//

#import "SLAddLockSetModelVC.h"

@interface SLAddLockSetModelVC ()

@end

@implementation SLAddLockSetModelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"Add new device");
    NSString *underLineStr = kLocalString(@"not remain awake");
    _topLbl.text = kLocalString(@"Enable Energy Saving Mode?");
    NSString *originalText = kLocalString(@"When 'Energy Saving Mode' is enabled, the padlock will operate in a power-saving mode to maximize battery life. With this feature enabled, the padlock will not remain awake, meaning you will need to touch the fingerprint sensor to wake it up before accessing the log or unlocking remotely via the app.");
    // 创建 NSMutableAttributedString
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:originalText];
    // 获取子字符串的范围
    NSRange underlineRange = [originalText rangeOfString:underLineStr];
    // 加下划线
    [attributedText addAttribute:NSUnderlineStyleAttributeName
                           value:@(NSUnderlineStyleSingle)
                           range:underlineRange];
    [attributedText addAttribute:NSForegroundColorAttributeName
                           value:[UIColor whiteColor]
                           range:underlineRange];
    [attributedText addAttribute:NSFontAttributeName
                           value:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium]
                           range:underlineRange];

    // 应用到 UILabel
    _tipLbl1.attributedText = attributedText;
    _tipLbl2.text = kLocalString(@"You can change this option later at any time in the padlock's settings menu.");
    _icon1.layer.cornerRadius = 3;
    _icon1.layer.masksToBounds = YES;
    _icon2.layer.cornerRadius = 3;
    _icon2.layer.masksToBounds = YES;
    
    if (kScreenHeight <= 667) {
        _topSpace.constant = 15;
    }
    _sureBtn.btnStyle = 1;
    [_sureBtn setTitle:kLocalString(@"Enable") forState:UIControlStateNormal];
    [_cancelBtn setTitle:kLocalString(@"Not now") forState:UIControlStateNormal];
}


- (IBAction)btnAction:(id)sender {
    if (sender == _cancelBtn) {
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[SLBluetoothManager sharedManager] setEnergyModel:NO finish:^(NSString *commond, BOOL isSuccess, id data, NSString *errorMsg) {
            if (!isSuccess) {
                kShowToast(errorMsg);
                [ZCTool hideLoadingOnKeyWindow];
                return;
            }
            kCurClickDevice.isOpenEnergyModel = NO;
            [ZKHttpManager sysncDeviceDataWith:@{@"isOpenEnergyModel":@"0"} finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
                [ZCTool hideLoadingOnKeyWindow];
                kNotifyRefreshDevcieList;
                [self.navigationController popToRootViewControllerAnimated:YES];
            }];
        }];
        
    } else {
        [ZCTool showLoadingOnKeyWindow];
        [ZCTool connectDeviceBackgroundWith:_model finish:^(BOOL isSuccess) {
            if (!isSuccess) {
                [ZCTool hideLoadingOnKeyWindow];
                return;
            }
            [[SLBluetoothManager sharedManager] setEnergyModel:YES finish:^(NSString *commond, BOOL isSuccess, id data, NSString *errorMsg) {
                if (!isSuccess) {
                    kShowToast(errorMsg);
                    [ZCTool hideLoadingOnKeyWindow];
                    return;
                }
                kCurClickDevice.isOpenEnergyModel = YES;
                [ZKHttpManager sysncDeviceDataWith:@{@"isOpenEnergyModel":@"1"} finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
                    [ZCTool hideLoadingOnKeyWindow];
                    kNotifyRefreshDevcieList;
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }];
            }];
        }];
    }
}


@end
